﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using LostLib.Algorithms;
using LostLib.IO;

namespace Tests.LostLib
{
    
    
    /// <summary>
    ///This is a test class for DiffUtilsTest and is intended
    ///to contain all DiffUtilsTest Unit Tests
    ///</summary>
	[TestClass()]
	public class DiffUtilsTest
	{


		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion


		/// <summary>
		///A test for Diff
		///</summary>
		[TestMethod()]
		public void DiffTestFiles()
		{
			var random = new Random();

			byte[] data = new byte[20];

			random.NextBytes(data);

			byte[] result;

			using(var originalPath = new TempFile())
			using(var newPath = new TempFile())
			using (var diffOutput = new TempFile())
			{
				File.WriteAllBytes(originalPath, data);

				data = new byte[15];
				random.NextBytes(data);
				File.WriteAllBytes(newPath, data);

				DiffUtils.Diff(originalPath, newPath, diffOutput);

				using (var patched = new TempFile())
				{
					DiffUtils.ApplyPatch(originalPath, patched, diffOutput);

					result = File.ReadAllBytes(patched);
				}
			}

			CollectionAssert.AreEqual(result, data, "applying patch resulted in incorrect data");
		}

		/// <summary>
		///A test for Diff and ApplyPatch on strings
		///</summary>
		[TestMethod()]
		public void DiffTestStrings()
		{
			string original = "// TODO: Initialize to an appropriate value"; // 
			string @new = "TODO: Initialize to an appropriate value 42";
			var diff = original.Diff(@new).ToArray();
			Assert.AreEqual(original.ApplyPatch(diff), @new);
		}
	}
}
